package com.bk.bookviews.common;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {


    @Bean
    public AuthInterceptor authInterceptor() {
        return new AuthInterceptor();
    }

    public void addInterceptors(InterceptorRegistry registry) {
        // 放行路径
        List<String> patterns = new ArrayList();
        patterns.add("/bk/userInfo/login");
        patterns.add("/bk/userInfo/register");
        patterns.add("/bk/scenic/scenicList");
        patterns.add("/bk/scenic");
        patterns.add("/bk/order/add");
        patterns.add("/bk/order/list");
        patterns.add("/bk/order/detail");
        patterns.add("/bk/notice/list");
        patterns.add("/static/image/**");
        // patterns.add("/bk/userInfo/userList");
//        patterns.add("/bk/userInfo");
//        patterns.add("/druid/**");
//        patterns.add("/sys/login");
//        patterns.add("/swagger/**");
//        patterns.add("/v2/api-docs");
//        patterns.add("/swagger-ui.html");
//        patterns.add("/swagger-resources/**");
//        patterns.add("/login");
        registry.addInterceptor(authInterceptor()).addPathPatterns("/**")
                .excludePathPatterns(patterns);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String realPath = new File("/static").getAbsolutePath().replace(File.separator, "//");

        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/").addResourceLocations("file:" + realPath + '/').addResourceLocations("file:/static/");
    }
}
